import userApi from "../../api/chaoxing/userApi/index";
import dbUserApi from "../../api/wechatCloud/dbUserApi/index";
import AES from "../../utils/AES";
import showToast from "../../utils/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    rules: {
      username: {
        require: {
          value: true,
          errMsg: "账号不能为空"
        },
      },
      password: {
        require: {
          value: true,
          errMsg: "密码不能为空"
        },
      }
    }
  },
  async onSubmit({ detail }: { detail: {username:string,password:string}}) {
    const params = await userApi.userLogin(detail.username, detail.password);
    if (typeof params == 'string') {
      showToast({title:params,icon:"none"})
      return;
    }
    userApi.storeUser(detail.username, params);
    const name = await userApi.getAccountInfo(params.uf!, params._d!, params._uid!, params.vc3!)
    getApp().globalData.curUser = detail.username
    await dbUserApi.setOrUpdateConfigByUsername({
      _id: detail.username,
      // 密码DES加密
      password: AES(detail.password),
      name,
      cookie: {
        _d: params._d!,
        uf: params.uf!,
        _uid: params._uid!,
        vc3: params.vc3!,
        fid: params.fid,
      },
    })
    wx.switchTab({
      url: '../index/index',
      success: () => {
        showToast({ title: `${name}登录成功` })
      }
    })
  },
});
